/**
 * 
 */
package com.dhcc.DRGsHosp.application.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.JdbcTemplateObj;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.DictionaryDataApplication;
import com.dhcc.DRGsHosp.core.domain.DictionaryData;
import com.dhcc.DRGsHosp.core.domain.DictionaryDataPrimary;
import com.dhcc.DRGsHosp.infra.util.StringUtils;

/**
 * @author chl
 *
 */
@Named
@Transactional
public class DictionaryDataApplicationImpl implements DictionaryDataApplication {
	
	private QueryChannelService queryChannel;
	private EntityRepository repository ;
	
	private EntityRepository getEntityRepository() {
		if(repository == null ){
			repository = InstanceFactory.getInstance(EntityRepository.class, "repository" ) ;
		}
		return repository;
	}
	private QueryChannelService getQueryChannelService(){
	       if(queryChannel==null){
	          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
	       }
	     return queryChannel;
	    }
   @Autowired  
    private JdbcTemplateObj jdbcTemplateObj; 
	/* (non-Javadoc)
	 * @see com.dhcc.DRGsHosp.application.DictionaryDataApplication#creatDictionaryData(com.dhcc.DRGsHosp.core.domain.DictionaryData)
	 */
	@Override
	public void creatDictionaryData(DictionaryData dictionaryData) {
		// TODO Auto-generated method stub
		dictionaryData.save();
	}


	/* (non-Javadoc)
	 * @see com.dhcc.DRGsHosp.application.DictionaryDataApplication#findAllDictionaryData()
	 */
	@Override
	public List<DictionaryData> findAllDictionaryData() {
		StringBuilder jpql = new StringBuilder() ;
		jpql.append("select dd from DictionaryData dd  ") ;
		List<DictionaryData> list = getQueryChannelService().createJpqlQuery(jpql.toString()).list() ;
		return list;
	}
	/**
	 *  @Description：删除列表中不存在的字典
	 *  @Author lhj 
	 *  @see com.dhcc.DRGsHosp.application.DictionaryDataApplication#delInvalidDictionaryDatas(java.lang.String, java.lang.String)
	 */
	@Override
	public void delInvalidDictionaryDatas(String param, String dicCode) {
		// 如果字典编码下没有数据，删除字典编码
		if(!StringUtils.hasLength(param)){
			String sql = " delete  from drgs.MR_DICTIONARYDATA t where t.dic_code= '"+dicCode+"' " ;
			jdbcTemplateObj.execute(sql);
		}else { // 如果字典编码下有数据
				String  sql = "delete  from drgs.MR_DICTIONARYDATA t where t.dic_code= '"+dicCode+"'  and t.dic_Value not in (" +param+")" ;
				jdbcTemplateObj.execute(sql);
		}
	}
	/**
	 *  @Description：保存字典详情
	 *  @Author lhj 
	 *  @see com.dhcc.DRGsHosp.application.DictionaryDataApplication#saveDictionaryData(java.util.Map)
	 */
	@Override
	public void saveDictionaryData(Map<String, Object> json) {
		 DictionaryData enity=new DictionaryData();
		 DictionaryDataPrimary enityP=new DictionaryDataPrimary();
		 String dicCode=json.get("dicCode")==null?null:json.get("dicCode").toString();
		 String dicValue=json.get("dicValue")==null?null:json.get("dicValue").toString();
		 String dicDesc=json.get("dicDesc")==null?null:json.get("dicDesc").toString();
		 String dicResume=json.get("dicResume")==null?null:json.get("dicResume").toString();
		 enityP.setDIC_CODE(dicCode);
		 enityP.setDIC_VALUE(dicValue);
		 enity.setPkId(enityP);
		 enity.setDicResume(dicResume);
		 enity.setDicDesc(dicDesc);
		 enity.save();	 	
	}
	/* (non-Javadoc)
	 * @see com.dhcc.DRGsHosp.application.DictionaryDataApplication#delDictionaryDataByCode(java.lang.String)
	 */
	@Override
	public void delDictionaryDataByCode(String dicCode) {
		String jpql = "delete from DictionaryData dd where dicCode = :dicCode " ;
		getEntityRepository().createJpqlQuery(jpql).addParameter("dicCode", dicCode).executeUpdate() ;
	}
	/* (non-Javadoc)
	 * @see com.dhcc.DRGsHosp.application.DictionaryDataApplication#getDictionaryDataById(java.lang.Integer)
	 */
	@Override
	public DictionaryData getDictionaryDataById(Integer id) {
		String jpql = "select dd from DictionaryData dd where dd.id = :id" ;
		DictionaryData dictionaryData = (DictionaryData) getEntityRepository().createJpqlQuery(jpql).addParameter("id", id).list().get(0) ;
		return dictionaryData;
	}
	@Override
	public Map<String, Object> findDicDataByDicCodeAndValue(String dicCode, String dicValue) {
		String sql="select t.dic_code diccode,t.dic_value dicvalue,t.dic_desc dicdesc,t.dic_resume dicresume from drgs.MR_DICTIONARYDATA t where 1=1 ";
		sql+=" and t.dic_code='"+dicCode+"' ";
		sql+=" and t.dic_value='"+dicValue+"' ";
		return jdbcTemplateObj.queryForMap(sql);
	}
	/**
	 * 
	 *  @Description：获取字典的code
	 *  @Author LiuHongJun  
	 *  @see com.dhcc.DRGsHosp.application.DictionaryDataApplication#findDictionaryCode()
	 */
	@Override
	public List<String> findDictionaryCode() {
		String sql="select distinct t.dic_code from drgs.MR_DICTIONARY t";
		return jdbcTemplateObj.queryForList(sql,String.class);
	}
	/**
	 *  @Description：获取明细字典
	 *  @Author lhj 
	 *  @see com.dhcc.DRGsHosp.application.DictionaryDataApplication#findDictionaryDataByCode(java.lang.String)
	 */
	@Override
	public List<Map<String, Object>> findDictionaryDataByCode(String dicCode) {
		String sql="select t.dic_code diccode,t.dic_value dicvalue,t.dic_desc dicdesc,t.dic_resume dicresume from drgs.MR_DICTIONARYDATA t where 1=1 ";
		if(StringUtils.hasLength(dicCode)){
			sql+=" and t.dic_code='"+dicCode+"' ";
		}
		return jdbcTemplateObj.queryForList(sql);
	}
}
